//////////////Start Cancel Reservation Page ////////////////
var oldObjArrayCancel = new Array();
function searchAll_cancelResvPage(value,e){
	/*if (e.keyCode == 13 && !e.ctrlKey) {
		getReservationRoomCancelResvManageMent("textSearch",value);
		$("#SearchAll_cancelresvPage").val("");
	}*/
}
function getReservationRoomCancelResvManageMent(data,valueSearch){
	var tab = getCurrentTabData();
	clearData();
	waitingDialog({});
	var searchName = $("#SearchAll_cancelresvPage").val();
	if($("#RowpagerCancelReservationListRoomManageMent").val() == null){
		tab.lineCancel = 10;
	}else{
		tab.lineCancel = $("#RowpagerCancelReservationListRoomManageMent").val();
	}
	if($("#pagerCancelReservationListRoomManageMent").val() == null){
		var pagePointer = 1; 
	}else{
		if(valueSearch!=""&&valueSearch!=undefined){
			var pagePointer = valueSearch; 
		}else{
			var pagePointer = 1;
			tab.pagerNumber = 1;
		}
	}
	var offfset = (tab.lineCancel*pagePointer)-tab.lineCancel;
	var dataOld =  $("#typeCheckReservationCancelReservation").val();
	var valueDateCheckin = $("#calendar_btw_checkinCancel").attr("dateofbtwcheckinCancel");
	var valueDateCheckout = $("#calendar_btw_checkoutCancel").attr("dateofbtwcheckoutCancel");
	
	var dateCheckin = new Date().toSystemFormat();
	var dateCheckout= new Date().toSystemFormat();
	if(valueDateCheckin!=""&&valueDateCheckout!=""){
		var dateCheckin = $("#calendar_btw_checkinCancel").attr("dateofbtwcheckinCancel");
		var dateCheckout = $("#calendar_btw_checkoutCancel").attr("dateofbtwcheckoutCancel");
	}
	$("#calendar_btw_checkinCancel").val($.datepicker.parseDate("yy-mm-dd",dateCheckin).getFormatedString());
	$("#calendar_btw_checkoutCancel").val($.datepicker.parseDate("yy-mm-dd",dateCheckout).getFormatedString());
	$('#calendar_btw_checkinCancel').attr('dateofbtwcheckinCancel',dateCheckin);
	$('#calendar_btw_checkoutCancel').attr('dateofbtwcheckoutCancel',dateCheckout);
	tab.valueDateCheckin = dateCheckin;
	tab.valueDateCheckout = dateCheckout;
	if(data=="CancelReservationListRoomManageMent"){
		if(pagePointer>1){
			var link = "/reservationService?func=getReservationAllCancelResv&offset="
				+offfset+"&line="+tab.lineCancel+"&pagePointer="+(pagePointer-1)+"&SearchAll_checkoutPage="+searchName;
			var linkSet = "/reservationService?func=getReservationAllCancelResv&offset="
				+offfset+"&line="+tab.lineCancel+"&pagePointer="+pagePointer+"&SearchAll_checkoutPage="+searchName;
			tab.linkSet = linkSet;
			simpleservice(link,null,showDataResRoomManageMentSetCountCancelReservation,tab.lineCancel);
		}else{
			tab.beforePageCancel = ""; 
			tab.beforeSubPageCancel = "";
			var link = "/reservationService?func=getReservationAllCancelResv&offset="
				+offfset+"&line="+tab.lineCancel+"&pagePointer="+pagePointer+"&SearchAll_checkoutPage="+searchName;
			simpleservice(link,null,showDataResRoomCancelResvManageMent,data);
		}
	}else if(data=="G" || data=="S" || data=="A"){
		tab.typeReservationCancel = data;
		typeReservationCancelFunction(dataOld,offfset,pagePointer,dateCheckin,dateCheckout,data,searchName)
	}else if(data == "textSearch"){
		showDataResRoomCancelResvManageMent("textSearch","textSearch");
	}else if(data == "dateSearch"){
		var dataOld =  $("#typeCheckReservationCancelReservation").val();
		var valueDateCheckin = $("#calendar_btw_checkinCancel").attr("dateofbtwcheckinCancel");
		var valueDateCheckout = $("#calendar_btw_checkoutCancel").attr("dateofbtwcheckoutCancel");
		if(valueDateCheckin!=""&&valueDateCheckout!=""){
			var dateCheckin = $("#calendar_btw_checkinCancel").attr("dateofBtwCheckinCancel");
			tab.valueDateCheckin = valueDateCheckin;
			tab.valueDateCheckout = valueDateCheckout;
			var dateCheckout= $("#calendar_btw_checkoutCancel").attr("dateofBtwCheckoutCancel");
			if(dataOld=="G" || dataOld=="S" || dataOld=="A"){
				var func = "";
				if(dataOld=="G") func = "getReservationAllCancelResvGroup";
				else if(dataOld=="S") func = "getReservationAllCancelResvSingle";
				else if(dataOld=="A") func = "getReservationAllCancelResv";
				tab.typeReservationCancel = dataOld;
				if(pagePointer>1){
					var link = "/reservationService?func="+func+"&offset="
					+offfset+"&line="+tab.lineCancel+"&pagePointer="+(pagePointer-1)+"&dateCheckin="
					+dateCheckin+"&dateCheckout="+dateCheckout+"&SearchAll_checkoutPage="+searchName;
					var linkSet = "/reservationService?func="+func+"&offset="
					+offfset+"&line="+tab.lineCancel+"&pagePointer="+pagePointer+"&dateCheckin="
					+dateCheckin+"&dateCheckout="+dateCheckout+"&SearchAll_checkoutPage="+searchName;
					tab.linkSet = linkSet;
					simpleservice(link,null,showDataResRoomManageMentSetCountCancelReservation,tab.lineCancel);
				}else{
					tab.beforePageCancel = ""; 
					tab.beforeSubPageCancel = "";
					var link = "/reservationService?func="+func+"&offset="
					+offfset+"&line="+tab.lineCancel+"&pagePointer="+pagePointer+"&dateCheckin="
					+dateCheckin+"&dateCheckout="+dateCheckout+"&SearchAll_checkoutPage="+searchName;
					simpleservice(link,null,showDataResRoomCancelResvManageMent,data);
				}
			}
		}else{
			$("#calendar_btw_checkinCancel").select();
		}
	}
}
function showDataResRoomCancelResvManageMent(data,output){
	var tab = getCurrentTabData();
	var valueDateCheckin = tab.valueDateCheckin;
	var valueDateCheckout = tab.valueDateCheckout;
	if(valueDateCheckin!=""&&valueDateCheckout!=""){
		var dateCheckin = valueDateCheckin;
		var dateCheckout = valueDateCheckout;
		$("#calendar_btw_checkinCancel").val($.datepicker.parseDate("yy-mm-dd",dateCheckin).getFormatedString());
		$("#calendar_btw_checkoutCancel").val($.datepicker.parseDate("yy-mm-dd",dateCheckout).getFormatedString());
		$('#calendar_btw_checkinCancel').attr('dateofbtwcheckinCancel',dateCheckin);
		$('#calendar_btw_checkoutCancel').attr('dateofbtwcheckoutCancel',dateCheckout);
	}
	// set for pager
	tab.tablePager = "tablepagingCancelReservation";
	tab.pagerNumberField = "pagerCancelReservationListRoomManageMent";
	tab.functionGetData = "getReservationRoomCancelResvManageMent";
	tab.pageName = "CancelReservationListRoomManageMent";
	tab.pageNavPosition = "pageNavPositionCancelReservation";
	tab.trCheck = "trCancelReservation";
	if(tab.cancelrevsCheckin!=undefined){
		$('#calendar_btw_checkinCancel').val($.datepicker.parseDate("yy-mm-dd",tab.cancelrevsCheckin).getFormatedString());
		$('#calendar_btw_checkinCancel').attr('dateofbtwcheckinCancel',tab.cancelrevsCheckin);
	}
	if(tab.cancelrevsCheckout!=undefined){
		$('#calendar_btw_checkoutCancel').val($.datepicker.parseDate("yy-mm-dd",tab.cancelrevsCheckout).getFormatedString());
		$('#calendar_btw_checkoutCancel').attr('dateofBtwCheckoutCancel',tab.cancelrevsCheckout);
	}
	if(tab.typeReservationCancel!=undefined){
		$('#typeCheckReservationCancelReservation option[value='+tab.typeReservationCancel+']').attr('selected', 'selected');
	}
	if(data.trim()=="[]"){
		var textDataEmpty = '<tr clearTr="cancelReservation" >'+
		'<td colspan="100%" style="text-align:center;" >'+
		dataEmpty+
		'</td>'+
		'</tr>';
		textRoomCancelResv = textDataEmpty;
		$("[clearTr=cancelReservation]").remove();
		$("#CancelResvRoomManageMent_Id_Tr").replaceWith("<tr id=CancelResvRoomManageMent_Id_Tr ></tr>"+textRoomCancelResv);
		textRoomCancelResv = '';
	}else{
		$("[clearTr=cancelReservation]").remove();
		var objArray = new Array();
		if((data=="textSearch"||data=="G"||data=="S"||data=="A")&&data!="dateSearch"){
			objArray = oldObjArrayCancel;
			objArray = objArray[0];
		}else{
			objArray = JSON.parse(data);
			oldObjArrayCancel = objArray;
			objArray = objArray[0];
		}
		sortIdRoommanagement(objArray);
		
		var numberSet = tab.beforePageCancel; 
		var numberSubSet = tab.beforeSubPageCancel;
		textRoomCancelResvPrint = '';
		var checkBgColor = 0;
		var countGroup = 0;
		var OldGroupId =0;
		var valueSearch = $("#SearchAll_cancelresvPage").val();
		var typeReservation =  $("#typeCheckReservationCancelReservation").val();
		var firstSelect = "";
		for(var i =0;i<objArray.length;i++){
			if(objArray[i].name!=""&&objArray[i].name!=undefined){
				var nameReservation = objArray[i].name.split(" ");
			}else{
				var nameReservation = "";
			}
			var Objtitle = objArray[i].reservationTitle;
			var ObjgroupId = objArray[i].groupId;
			var ObjId = objArray[i].id;
			var checkContinue = checkedFilter(output,valueSearch,nameReservation,firstSelect,Objtitle,ObjgroupId,typeReservation,ObjId);
			if(checkContinue==1){
				continue;
			}
			if(objArray[i].groupId!="0"){
				if(objArray[i].subList!=undefined){
					//OldGroupId = objArray[i].groupIdString;
					var setDateFormat = new Date(objArray[i].date);
					var setFateFormatCheckout = setDateFormat.addDate(objArray[i].span);
					groupId = objArray[i].groupIdString;
					var splitResIdInGroup = objArray[i].resList.split(",");
					var textSubRes = "";
					var textSubResPrint = "";
					var checkBgColorG =0;
					var StatusResInGroup = "";
					for(var subRes = 0;subRes<objArray[i].subList.length;subRes++){
						var CheckIn = '<span class="link" onClick="editAfterResCancellation('+objArray[i].subList[subRes].id+')">'+Reinstate+',</span><br>';
						var Cancel = '<span class="link" id="'+objArray[i].subList[subRes].id+'"  onClick="sendMailCancel('+objArray[i].subList[subRes].id+')">'+cancellationEmail+'</span>';
						var linkReservation = "";
						if(objArray[i].subList[subRes].reservationTitle=="Cancel Reservation"){
							linkReservation	= '<td width="5%" class="spanDataCenter">'+
							'<div onmouseover="linkClick=false;" onmouseout="linkClick=true;">'+
									'<span>'+CheckIn+' '+Cancel+'</span>'+
								'</div>'+
							'</td>';
						}else{
							linkReservation	= '<td width="5%" class="spanDataCenter">'+
							'<div onmouseover="linkClick=false;" onmouseout="linkClick=true;">'+
								'</div>'+
							'</td>';
						}
						var setDateFormatRes = new Date(objArray[i].subList[subRes].date);
						var setFateFormatCheckoutRes = setDateFormatRes.addDate(objArray[i].subList[subRes].span);
						var ShowNameGuest = "";
						/// Name Empty
						if(objArray[i].subList[subRes].name!=undefined){
							ShowNameGuest = objArray[i].subList[subRes].name
						}
						///
						if(objArray[i].subList[subRes].reservationTitle=="Reserved Rooms"){
							StatusResInGroup = statusResv;
						}else if(objArray[i].subList[subRes].reservationTitle=="Checked-out Rooms"){
							StatusResInGroup = statusCheckOut;
						}else if(objArray[i].subList[subRes].reservationTitle=="Temp Rooms"){
							StatusResInGroup = statusTempResv;
						}else if(objArray[i].subList[subRes].reservationTitle=="Cancel Reservation"){
							StatusResInGroup = statusCancel;
						}else{
							StatusResInGroup = statusCheckin;
						}
						if(checkBgColorG==0){
							statusRecord = 'dataRecordBot';
							checkBgColorG = 1;
						}else{
							statusRecord = 'dataRecordBot';
							checkBgColorG =0;
						}
						var showDisplayGuestname = "";
						if(ShowNameGuest==""){
							showDisplayGuestname = '<tr clearTr=cancelReservation class="'+statusRecord+'" onclick="if(linkClick==true)noGuestName();"   id="btnEditS" >';
						}else{
							showDisplayGuestname = '<tr clearTr=cancelReservation class="'+statusRecord+'" onclick="if(linkClick) CallEditReservedPageWithLoadElement(id,'+objArray[i].subList[subRes].id+')"  id="btnEditS" >';
						}
						var runnumberSub = "";
						if(getCurrentTabData().beforePageCancel!=undefined&&getCurrentTabData().beforePageCancel!=""){
							runnumberSub = (numberSet+1)+"."+(subRes+1);
						}else{
							runnumberSub = (i+1)+"."+(subRes+1);
						}
							textSubRes += showDisplayGuestname+
							'<td colspan="2" class="spanDataPaddingLeftSub">'+
							runnumberSub+
							'</td>'+
							'<td class="spanDataCenter">' +
								'<span>'+objArray[i].subList[subRes].id+'</span>'+
							'</td>'+
							'<td class="spanDataCenter">'+
								'<span class="guestName" >'+ShowNameGuest+'</span>'+
							'</td>'+
							'<td class="spanDataCenter">'+
								'<span>'+setDateFormatRes.getFormatedString()+'-'+setFateFormatCheckoutRes.getFormatedString()+' / '+objArray[i].subList[subRes].span+'</span>'+
							'</td>'+
							'<td class="spanDataCenter"> '+
								'<span>'+objArray[i].subList[subRes].roomTypeName+'('+objArray[i].subList[subRes].roomName+')'+'</span>'+
							'</td>'+
							'<td class="spanDataCenter">' +
								'<span>'+adult+'('+objArray[i].subList[subRes].adult+') - '+child+'('+objArray[i].subList[subRes].child+')'+'</span>'+
							'</td>'+
							'<td class="spanDataCenter">'+
								StatusResInGroup+
							'</td>'+
							'<td class="spanDataCenter">'+
								'<span> '+toCurrencyString(objArray[i].subList[subRes].defaultCherge)+'</span>'+//////
							'</td>'+
							'<td class="spanDataRight">'+
								'<span><div class="fongwordwarp" >'+toCurrencyString(currencyStringToNumber(objArray[i].subList[subRes].ChargeCheckTemp))+'<br>'+/////
								'(ยอดคงเหลือ '+toCurrencyString(objArray[i].subList[subRes].afterNightChargeRoom)+')</div></span>'+/////
							'</td>'+
							'<td class="spanDataCenter" style="color:#5B82BC;">'+
								'<span>'+objArray[i].subList[subRes].reasoncancelString+'</span>'+
							'</td>'+
							linkReservation+
							'</tr>';
							textSubResPrint += '<tr clearTr="cancelReservation" class="dataRecord" onclick="if(linkClick&&ShowNameGuest!="") CallEditReservedPageWithLoadElement(id,'+objArray[i].subList[subRes].id+')"  id="btnEditS" >'+
							'<td colspan="2" class="spanDataPaddingLeftSub">'+
							runnumberSub+
							'</td>'+
							'<td class="spanDataCenter">' +
								'<span>'+objArray[i].subList[subRes].id+'</span>'+
							'</td>'+
							'<td class="spanDataCenter">'+
								'<span class="guestName" >'+ShowNameGuest+'</span>'+
							'</td>'+
							'<td class="spanDataCenter">'+
								'<span>'+setDateFormatRes.getFormatedString()+'-'+setFateFormatCheckoutRes.getFormatedString()+' / '+objArray[i].subList[subRes].span+'</span>'+
							'</td>'+
							'<td class="spanDataCenter"> '+
								'<span>'+objArray[i].subList[subRes].roomTypeName+'('+objArray[i].subList[subRes].roomName+')'+'</span>'+
							'</td>'+
							'<td class="spanDataCenter">' +
								'<span>'+adult+'('+objArray[i].subList[subRes].adult+') - '+child+'('+objArray[i].subList[subRes].child+')'+'</span>'+
							'</td>'+
							'<td class="spanDataCenter">'+
								StatusResInGroup+
							'</td>'+
							'<td class="spanDataCenter">'+
							'<span> '+objArray[i].subList[subRes].defaultCherge+'</span>'+/////
							'</td>'+
							'<td class="spanDataRight">'+
								'<span><div class="fongwordwarp" >'+objArray[i].subList[subRes].ChargeCheckTemp+'<br>'+/////
								'(ยอดคงเหลือ '+objArray[i].subList[subRes].afterNightChargeRoom+')</div></span>'+/////
							'</td>'+
							'<td class="spanDataCenter">'+
								objArray[i].subList[subRes].reasoncancelString+
							'</td>'+
							'</tr>';
					}
					var obj = objArray[i].subList;
					showStatusResInGroup = checkStatusReservationGroup(obj);
					if(getCurrentTabData().beforePageCancel!=undefined&&getCurrentTabData().beforePageCancel!=""){
						numberSet = numberSet+1;
						number = numberSet;
					}else{
						number = (i+1);
					}
						textRoomCancelResv += '<tr clearTr="cancelReservation" class="dataRecordGroup" onclick="if(linkClick) CallEditReservedPageWithLoadElement(id,'+objArray[i].groupIdString+')" id="btnEditG" >'+
						'<td class="spanDataLeft">'+
						number+'(<span id=groupCount'+objArray[i].groupIdString+' >'+objArray[i].resList.split(",").length+'</span>)'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>G'+objArray[i].groupIdString+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">' +
							'<span></span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span class="ownerName" >'+objArray[i].name+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>'+setDateFormat.getFormatedString()+'-'+setFateFormatCheckout.getFormatedString()+' / '+objArray[i].span+'</span>'+
						'</td>'+
						'<td class="spanDataCenter"> '+
						'</td>'+
						'<td class="spanDataCenter">' +
							'<span></span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
						'<span>'+showStatusResInGroup+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>'+toCurrencyString(objArray[i].totaltax)+'</span>'+/////
						'</td>'+
						'<td class="spanDataRight">'+
							'<span><div class="fongwordwarp" >'+toCurrencyString(currencyStringToNumber(objArray[i].ChargeCheckTemp))+'<br>'+/////
							'(ยอดคงเหลือ '+toCurrencyString(objArray[i].afterNightChargeRoom)+')</div></span>'+/////
						'</td>'+
						'<td class="spanDataCenter">'+
						'</td>'+
						'<td class="spanDataCenter">'+
						'</td>'+
					'</tr>'+textSubRes;
						textRoomCancelResvPrint += '<tr clearTr="cancelReservation" class="dataRecordGroup">'+
						'<td class="spanDataLeft">'+
						number+'(<span id=groupCount'+objArray[i].groupIdString+' >'+objArray[i].resList.split(",").length+'</span>)'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>G'+objArray[i].groupIdString+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">' +
							'<span></span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span class="ownerName" >'+objArray[i].name+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>'+setDateFormat.getFormatedString()+'-'+setFateFormatCheckout.getFormatedString()+' / '+objArray[i].span+'</span>'+
						'</td>'+
						'<td class="spanDataCenter"> '+
						
						'</td>'+
						'<td class="spanDataCenter">' +
							'<span></span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
						'<span>'+showStatusResInGroup+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>'+toCurrencyString(objArray[i].totaltax)+'</span>'+/////
						'</td>'+
						'<td class="spanDataRight">'+
							'<span><div class="fongwordwarp" >'+toCurrencyString(currencyStringToNumber(objArray[i].ChargeCheckTemp))+'<br>'+/////
							'(ยอดคงเหลือ '+toCurrencyString(objArray[i].afterNightChargeRoom)+')</div></span>'+/////
						'</td>'+
						'<td class="spanDataCenter">'+
						'</td>'+
						'<td class="spanDataCenter">'+
						'</td>'+
					'</tr>'+textSubResPrint;
				}else{
					var CheckIn = '<span class="link" onClick="editAfterResCancellation('+objArray[i].id+')">'+Reinstate+',</span><br>';
					var Cancel = '<span class="link" id="'+objArray[i].id+'"  onClick="sendMailCancel('+objArray[i].id+')">'+cancellationEmail+'</span>';
					var linkReservation = "";
					if(objArray[i].reservationTitle=="Cancel Reservation"){
						linkReservation	= '<td width="5%" class="spanDataCenter">'+
						'<div onmouseover="linkClick=false;" onmouseout="linkClick=true;">'+
								'<span>'+CheckIn+' '+Cancel+'</span>'+
							'</div>'+
						'</td>';
					}else{
						linkReservation	= '<td width="5%" class="spanDataCenter">'+
						'<div onmouseover="linkClick=false;" onmouseout="linkClick=true;">'+
							'</div>'+
						'</td>';
					}
					var setDateFormatRes = new Date(objArray[i].date);
					var setFateFormatCheckoutRes = setDateFormatRes.addDate(objArray[i].span);
					var ShowNameGuest = "";
					/// Name Empty
					if(objArray[i].name!=undefined){
						ShowNameGuest = objArray[i].name
					}
					///
					if(objArray[i].reservationTitle=="Reserved Rooms"){
						StatusResInGroup = statusResv;
					}else if(objArray[i].reservationTitle=="Checked-out Rooms"){
						StatusResInGroup = statusCheckOut;
					}else if(objArray[i].reservationTitle=="Temp Rooms"){
						StatusResInGroup = statusTempResv;
					}else if(objArray[i].reservationTitle=="Cancel Reservation"){
						StatusResInGroup = statusCancel;
					}else{
						StatusResInGroup = statusCheckin;
					}
					if(checkBgColorG==0){
						statusRecord = 'dataRecordBot';
						checkBgColorG = 1;
					}else{
						statusRecord = 'dataRecordBot';
						checkBgColorG =0;
					}
					var showDisplayGuestname = "";
					if(ShowNameGuest==""){
						showDisplayGuestname = '<tr clearTr=cancelReservation class="'+statusRecord+'"   id="btnEditS" >';
					}else{
						showDisplayGuestname = '<tr clearTr=cancelReservation class="'+statusRecord+'" onclick="if(linkClick) CallEditReservedPageWithLoadElement(id,'+objArray[i].id+')"  id="btnEditS" >';
					}
					var runnumberSub = "";
					if(getCurrentTabData().beforePageCancel!=undefined&&getCurrentTabData().beforePageCancel!=""){
						runnumberSub = (numberSet)+"."+(numberSubSet);
						numberSubSet = numberSubSet+1;
					}else{
						runnumberSub = (i+1)+"."+(subRes+1);
					}
					textRoomCancelResv += showDisplayGuestname+
						'<td colspan="2" class="spanDataPaddingLeftSub">'+
						runnumberSub+
						'</td>'+
						'<td class="spanDataCenter">' +
							'<span>'+objArray[i].id+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span class="guestName" >'+ShowNameGuest+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>'+setDateFormatRes.getFormatedString()+'-'+setFateFormatCheckoutRes.getFormatedString()+' / '+objArray[i].span+'</span>'+
						'</td>'+
						'<td class="spanDataCenter"> '+
							'<span>'+objArray[i].roomTypeName+'('+objArray[i].roomName+')'+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">' +
							'<span>'+adult+'('+objArray[i].adult+') - '+child+'('+objArray[i].child+')'+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							StatusResInGroup+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span> '+toCurrencyString(objArray[i].defaultCherge)+'</span>'+/////
						'</td>'+
						'<td class="spanDataRight">'+
							'<span><div class="fongwordwarp" >'+objArray[i].ChargeCheckTemp+'<br>'+/////
							'(ยอดคงเหลือ '+objArray[i].afterNightChargeRoom+')</div></span>'+/////
						'</td>'+
						'<td class="spanDataCenter" style="color:#5B82BC;">'+
							'<span>'+objArray[i].reasoncancelString+'</span>'+
						'</td>'+
						linkReservation+
						'</tr>';
					textRoomCancelResvPrint += '<tr clearTr="cancelReservation" class="dataRecord" onclick="if(linkClick&&ShowNameGuest!="") CallEditReservedPageWithLoadElement(id,'+objArray[i].id+')"  id="btnEditS" >'+
						'<td colspan="2" class="spanDataPaddingLeftSub">'+
						runnumberSub+
						'</td>'+
						'<td class="spanDataCenter">' +
							'<span>'+objArray[i].id+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span class="guestName" >'+ShowNameGuest+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>'+setDateFormatRes.getFormatedString()+'-'+setFateFormatCheckoutRes.getFormatedString()+' / '+objArray[i].span+'</span>'+
						'</td>'+
						'<td class="spanDataCenter"> '+
							'<span>'+objArray[i].roomTypeName+'('+objArray[i].roomName+')'+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">' +
							'<span>'+adult+'('+objArray[i].adult+') - '+child+'('+objArray[i].child+')'+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							StatusResInGroup+
						'</td>'+
						'<td class="spanDataCenter">'+
						'<span> '+toCurrencyString(objArray[i].defaultCherge)+'</span>'+/////
						'</td>'+
						'<td class="spanDataRight">'+
							'<span><div class="fongwordwarp" >'+objArray[i].ChargeCheckTemp+'<br>'+/////
							'(ยอดคงเหลือ '+objArray[i].afterNightChargeRoom+')</div></span>'+/////
						'</td>'+
						'<td class="spanDataCenter">'+
							objArray[i].reasoncancelString+
						'</td>'+
						'</tr>';
				}
			}else{
				res = objArray[i].id;
				var setDateFormat = new Date(objArray[i].date);
				var setFateFormatCheckout = setDateFormat.addDate(objArray[i].span);
				getStatusResInGroup = objArray[i].reservationTitle;
				if(getStatusResInGroup=="Reserved Rooms"){
					showStatusResInGroup = statusResv;
				}else if(getStatusResInGroup=="Checked-out Rooms"){
					showStatusResInGroup = statusCheckOut;
				}else if(getStatusResInGroup=="Temp Rooms"){
					showStatusResInGroup = statusTempResv;
				}else if(getStatusResInGroup=="Cancel Reservation"){
					showStatusResInGroup = statusCancel;
				}else{
					showStatusResInGroup = statusCheckin;
				}
				if(checkBgColorG==0)
					checkBgColorG = 1;
				else
					checkBgColorG =0;
				statusRecord = 'dataRecordBot';
				
				res = objArray[i].id;
				var CheckIn = '<span class="link" onClick="editAfterResCancellation('+res+')">'+Reinstate+',</span><br>';
				var Cancel = '<span class="link" id="'+res+'"  onClick="sendMailCancel('+res+')">'+cancellationEmail+'</span>';
				var linkReservation = "";
				if(objArray[i].reservationTitle=="Cancel Reservation"){
					linkReservation	= '<td class="spanDataCenter">'+
					'<div onmouseover="linkClick=false;" onmouseout="linkClick=true;">'+
							'<span>'+CheckIn+' '+Cancel+'</span>'+
						'</div>'+
					'</td>';
				}else{
					linkReservation	= '<td class="spanDataCenter">'+
					'<div onmouseover="linkClick=false;" onmouseout="linkClick=true;">'+
						'</div>'+
					'</td>';
				}
				if(getCurrentTabData().beforePageCancel!=undefined&&getCurrentTabData().beforePageCancel!=""){
					numberSet = numberSet+1;
					number = numberSet;
				}else{
					number = (i+1);
				}
				if(setDateFormat.getFormatedString()!=undefined){
						textRoomCancelResv += '<tr clearTr="cancelReservation" class="'+statusRecord+'" onclick="if(linkClick){CallEditReservedPageWithLoadElement(id,'+objArray[i].id+')}" id="btnEditS">'+
						'<td class="spanDataCenter" >'+
						number+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span></span>'+
						'</td>'+
						'<td class="spanDataCenter">' +
							'<span>'+objArray[i].id+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span class="guestName" >'+objArray[i].name+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>'+setDateFormat.getFormatedString()+'-'+setFateFormatCheckout.getFormatedString()+' / '+objArray[i].span+'</span>'+
						'</td>'+
						'<td class="spanDataCenter"> '+
							'<span>'+objArray[i].roomTypeName+'('+objArray[i].roomName+')'+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">' +
						'<span>'+adult+'('+objArray[i].adult+') - '+child+'('+objArray[i].child+')'+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>'+showStatusResInGroup+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>'+toCurrencyString(objArray[i].defaultCherge)+'</span>'+/////
						'</td>'+
						'<td class="spanDataRight">'+
							'<span>'+toCurrencyString(currencyStringToNumber(objArray[i].ChargeCheckTemp))+'<br>'+/////
							'(ยอดคงเหลือ '+toCurrencyString(objArray[i].afterNightChargeRoom)+')</span>'+/////
						'</td>'+
						'<td class="spanDataCenter" style="color:#5B82BC;">'+
							objArray[i].reasoncancelString+
						'</td>'+
						linkReservation+
					'</tr>';
						textRoomCancelResvPrint += '<tr clearTr="cancelReservation" class="dataRecord" >'+
						'<td class="spanDataCenter" >'+
						number+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span></span>'+
						'</td>'+
						'<td class="spanDataCenter">' +
							'<span>'+objArray[i].id+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span class="guestName" >'+objArray[i].name+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>'+setDateFormat.getFormatedString()+'-'+setFateFormatCheckout.getFormatedString()+' / '+objArray[i].span+'</span>'+
						'</td>'+
						'<td class="spanDataCenter"> '+
							'<span>'+objArray[i].roomTypeName+'('+objArray[i].roomName+')'+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">' +
						'<span>'+adult+'('+objArray[i].adult+') - '+child+'('+objArray[i].child+')'+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>'+showStatusResInGroup+'</span>'+
						'</td>'+
						'<td class="spanDataCenter">'+
							'<span>'+toCurrencyString(objArray[i].defaultCherge)+'</span>'+/////
						'</td>'+
						'<td class="spanDataRight">'+
							'<span>'+toCurrencyString(currencyStringToNumber(objArray[i].ChargeCheckTemp))+'<br>'+/////
							'(ยอดคงเหลือ '+toCurrencyString(objArray[i].afterNightChargeRoom)+')</span>'+/////
						'</td>'+
						'<td class="spanDataCenter" style="color:#5B82BC;">'+
							objArray[i].reasoncancelString+
						'</td>'+
						'<td class="spanDataCenter">'+
						'</td>'+
					'</tr>';
				}
			}
		}
		if(textRoomCancelResv==""){
			var textDataEmpty = '<tr clearTr="cancelReservation" >'+
			'<td colspan="100%" style="text-align:center;" >'+
			dataEmpty+
			'</td>'+
			'</tr>';
			textRoomCancelResv = textDataEmpty;
		}
		$("#CancelResvRoomManageMent_Id_Tr").replaceWith('<tr id=CancelResvRoomManageMent_Id_Tr ></tr>'+textRoomCancelResv);
		textRoomCancelResv = "";
		textSubRes = "";
		textPrint = textRoomCancelResvPrint;
	}
	dateRoomManageMent({checkin:"calendar_btw_checkinCancel",atrcheckin:"dateofBtwCheckinCancel"},{checkout:"calendar_btw_checkoutCancel",atrcheckout:"dateofBtwCheckoutCancel"});
	$(".ui-datepicker-trigger").css("margin-top","2px");
	closeWaitingDialog();
	showDataResRoomManageMentSetPageCancelReservation(data,getCurrentTabData().lineCancel);
}

function showDataResRoomManageMentSetPageCancelReservation(data,output){
	$("[clearTr*=cancelReservationPage]").remove();
	objArray = JSON.parse(data);
	objArray = objArray[1];
	
	var textRoomCancelSetpage = "";
	for(var i =0;i<objArray.length;i++){
		var textSubRes = "";
		if(objArray[i].groupId!="0"){
			var statusRecord = "";
			for(var subRes = 0;subRes<objArray[i].subList.length;subRes++){
				textSubRes += '<tr clearTr=cancelReservationPage  ><td></td></tr>';
			}
			textRoomCancelSetpage += '<tr clearTr=cancelReservationPage class="trCancelReservation"><td></td></tr>'+textSubRes;
		}else{
			textRoomCancelSetpage += '<tr clearTr=cancelReservationPage ><td></td></tr>';
		}
	}
	$("[name = CancelResvRoomManageMent_Id_Tr2]").replaceWith("<tr name=CancelResvRoomManageMent_Id_Tr2></tr>"+textRoomCancelSetpage);
	textRoomCancelSetpage = "";
	textSubRes = "";
	showPager(output);
}

function showDataResRoomManageMentSetCountCancelReservation(data,output){
	var subCount = 0;
	var recCount = 0;
	var objArray = new Array();
	if((data=="textSearch"||data=="G"||data=="S"||data=="A"||data=="reservationType_A"||data=="Checked-out Rooms"||data=="Checked-in Rooms")&&data!="dateSearch"){
		objArray = oldObjArrayCheckin;
		objArrayCheck = objArray[0];
		objArray = objArray[1];
	}else{
		objArray = JSON.parse(data);
		oldObjArrayCheckin = objArray;
		objArrayCheck = objArray[0];
		objArray = objArray[1];
	}
	setRowObjArrayCheckin = objArray;
	sortIdRoommanagement(objArray);
	sortIdRoommanagement(objArrayCheck);
	for(var i =0;i<objArray.length;i++){
		if(objArray[i].groupId!="0"){
			if(objArray[i].subList!=undefined){
				if(objArrayCheck[objArrayCheck.length-1].groupId==objArray[i].groupId){
					recCount = (i+1);
					subCount = objArrayCheck[objArrayCheck.length-1].subList.length+1;
					break;
				}
			}else{
				if(objArrayCheck[objArrayCheck.length-1].groupId==objArray[i].groupId){
					recCount = (i+1);
					break;
				}
			}
		}else{
			if(objArrayCheck[objArrayCheck.length-1].id==objArray[i].id){
				recCount = (i+1);
				break;
			}
		}
	}
	getCurrentTabData().beforePageCancel = (recCount);
	getCurrentTabData().beforeSubPageCancel = subCount;
	simpleservice(getCurrentTabData().linkSet,null,showDataResRoomCancelResvManageMent,data);
}

function DeleteReservation(){
	
	var Tabs = getCurrentTabData();
	if($("#resvList tr[resId] input[type=checkbox]:checked").length > 0)
	{
		confirmDialog(Popup_confirmCancellation,function() {
			FillCancelReservationReason();
		});
	}
}

function FillCancelReservationReason(){
	var content = $('<table class="simpleTable"><tr>' +
			'<td style="width:80px;">'+Popup_label_Cancel_Reason+' : </td>' +
			'<td><div style="display:table-cell;"><span class="margin-right"><select id="insert-reason-reason"></select></span>' +
			'<span>Charge : <input type="text" class="currency" id="insert-reason-cost"> <span name="unit-money"> '+SharedData.General.defaultCurrency.symbol+'</span></div></td>' +
		'</tr>'+
		'<tr><td>&nbsp;</td><td><span name="displayTime" class="link">'+Popup_label_insert_time+'</span></td></tr>' +
		'<tr name="time" style="display:none;"><td>&nbsp;</td><td>' +
			'<select></select> : <select></select>'+Popup_label_time+
		'</td></tr>' +
		'<tr><td>'+Popup_label_Cancel_detail+' : </td>' +
			'<td><textarea id="insert-cancel-resondetail" rows="2" cols="45" ></textarea>' +
		'</td></tr>' +
		'<tr><td></td><td>' +
			'<div style="display:table-cell; float:left;"><input type="checkbox" name="insert-reason-send-email" /> '+Popup_label_Cancel_SendEmail + '</div>' +
			'<div name="btnEditMail" class="link" style="float:right; display:table-cell; display:none; margin-top:4px;">'+Popup_label_Cancel_insert_mail+'</div>' +
		'</td></tr>' +
		'<tr name="insert-mail" style="display:none;"><td>&nbsp;</td>' +
			'<td><textarea id="insert-cancel-mail" rows="2" cols="45" ></textarea>' +
		'</td></tr>' +
		'</table>');

	//fill Reason
	for(var i=0;i<SharedData.CancelReason.length;i++)
		content.find("#insert-reason-reason").append(optionString.format(SharedData.CancelReason[i].id,SharedData.CancelReason[i].Description));
	for(var i=0;i<24;i++)
		content.find("[name='time'] select:first").append(optionString.format(i,i));
	for(var i=0;i<60;i+=5)
		content.find("[name='time'] select:nth-child(2)").append(optionString.format(i,i));
	content.find("#insert-reason-cost").val((0).toFixed(SharedData.General.currencyDecimalPlaces));
	content.find("#insert-reason-cost").attr("placeholder",(0).toFixed(SharedData.General.currencyDecimalPlaces));
	
	getServerDateTime();
	
	var buttons = [
		{ title:Popup_btn_Cancel_resv , callback: function(thisdialog,args){
			thisdialog.dialog("close");
			//callCancelReaservation();
		}},
		{ title:simpleLabel_Button_Close , callback: function(thisdialog,args){
			thisdialog.dialog("close");
		}}
	];
	var msgDialog = customDialog(Popup_ReasonCancellation_title,content.wrap('<div/>').parent().html(),{ width: 600},buttons,null);
	msgDialog.find("[name='displayTime']").click(function(){ msgDialog.find("[name='time']").css("display",""); });
	msgDialog.find("[name='btnEditMail']").click(function(){ msgDialog.find("[name='insert-mail']").css("display",""); });
	msgDialog.find("[name='insert-reason-send-email']").change(function(){
		if(this.checked)
			msgDialog.find("[name='btnEditMail']").css("display","");
		else{
			msgDialog.find("[name='btnEditMail']").css("display","none");
			msgDialog.find("[name='insert-mail']").css("display","none");
		}
	});
}

function typeReservationCancelFunction(dataOld,offfset,pagePointer,dateCheckin,dateCheckout,data,searchName){
	var tab = getCurrentTabData();
	if(dataOld=="G"||dataOld=="S"||dataOld=="A"){
		var func = "";
		if(dataOld=="G") func = "getReservationAllCancelResvGroup";
		else if(dataOld=="S") func = "getReservationAllCancelResvSingle";
		else if(dataOld=="A") func = "getReservationAllCancelResv";

		tab.typeReservationCancel = dataOld;
		if(pagePointer>1){
			var link = "/reservationService?func="+func+"&offset="
			+offfset+"&line="+tab.lineCancel+"&pagePointer="+(pagePointer-1)+"&dateCheckin="+dateCheckin+"&dateCheckout="+dateCheckout+"&SearchAll_checkoutPage="+searchName;
			var linkSet = "/reservationService?func="+func+"&offset="
			+offfset+"&line="+tab.lineCancel+"&pagePointer="+pagePointer+"&dateCheckin="+dateCheckin+"&dateCheckout="+dateCheckout+"&SearchAll_checkoutPage="+searchName;
			tab.linkSet = linkSet;
			simpleservice(link,null,showDataResRoomManageMentSetCountCancelReservation,tab.lineCancel);
		}else{
			tab.beforePageCancel = ""; 
			tab.beforeSubPageCancel = "";
			var link = "/reservationService?func="+func+"&offset="
			+offfset+"&line="+tab.lineCancel+"&pagePointer="+pagePointer+"&dateCheckin="+dateCheckin+"&dateCheckout="+dateCheckout+"&SearchAll_checkoutPage="+searchName;
			simpleservice(link,null,showDataResRoomCancelResvManageMent,data);
		}
	}
}